

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fly542">
  <meta name="keywords" content="">
  
    <meta name="description" content="还在为写一个makefile发愁吗？还在为难记的make参数而苦恼吗？有了scons一切都边的简单了。最近研究了下scons，发现和makefile相比，soncs编译c和c++程序真的太方便了。搞程序什么做好，当然是简单实用最好下面大家就一起感受下简单的scons吧。 1. 使用scons编译程序在程序目录下执行命令如下：（前提是你已经安装scons了哟） scons      噢，噢，这个好像">
<meta property="og:type" content="article">
<meta property="og:title" content="scons实用说明">
<meta property="og:url" content="http://fly542.cn/2014/08/01/04DevOps/05scons%E5%AE%9E%E7%94%A8%E8%AF%B4%E6%98%8E/index.html">
<meta property="og:site_name" content="Fly542 技术沉淀">
<meta property="og:description" content="还在为写一个makefile发愁吗？还在为难记的make参数而苦恼吗？有了scons一切都边的简单了。最近研究了下scons，发现和makefile相比，soncs编译c和c++程序真的太方便了。搞程序什么做好，当然是简单实用最好下面大家就一起感受下简单的scons吧。 1. 使用scons编译程序在程序目录下执行命令如下：（前提是你已经安装scons了哟） scons      噢，噢，这个好像">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2014-08-01T13:23:55.000Z">
<meta property="article:modified_time" content="2022-12-30T03:33:01.425Z">
<meta property="article:author" content="Fly542">
<meta property="article:tag" content="scons">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>scons实用说明 - Fly542 技术沉淀</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"fly542.cn","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fly542 技术沉淀</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="scons实用说明">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2014-08-01 21:23" pubdate>
        2014年8月1日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.9k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      16 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">scons实用说明</h1>
            
            <div class="markdown-body">
              <p>还在为写一个makefile发愁吗？还在为难记的make参数而苦恼吗？有了scons一切都边的简单了。最近研究了下scons，发现和makefile相比，soncs编译c和c++程序真的太方便了。搞程序什么做好，当然是简单实用最好下面大家就一起感受下简单的scons吧。</p>
<h2 id="1-使用scons编译程序"><a href="#1-使用scons编译程序" class="headerlink" title="1. 使用scons编译程序"></a>1. 使用scons编译程序</h2><p>在程序目录下执行命令如下：（前提是你已经安装scons了哟）</p>
<p><code>scons</code>     </p>
<p>噢，噢，这个好像没啥简单的哈，和make差不多嘛。是吗？那就继续瞧好吧您.</p>
<h2 id="2-清空编译程序"><a href="#2-清空编译程序" class="headerlink" title="2. 清空编译程序"></a>2. 清空编译程序</h2><p>在程序目录下执行命令如下</p>
<p><code>scons -c</code></p>
<p>这个嘛，，没什么吧，和make clean也没简单多少呀，不就少了几个字母吗，是吗？那咱还继续吧</p>
<h2 id="3-将cpp或c文件编译为执行文件"><a href="#3-将cpp或c文件编译为执行文件" class="headerlink" title="3. 将cpp或c文件编译为执行文件"></a>3. 将cpp或c文件编译为执行文件</h2><p> 在源文件目录中创建一个SConstruct的文件，并在其中添加如下内容</p>
<p><code>Program(&#39;testPro1&#39;, Glob(&#39;*.cpp&#39;))</code></p>
<p> 保存后执行1中的scons命令</p>
<p> 这就完事了？？是的！这就完事了，只要你程序的语法没错，应该会生成一个名字叫testPro1的程序，如果在window下会生成一个testPro1.exe的程序哟，有没有很酷？</p>
<p>  将cpp或c文件编译为执行文件有如下2中方式</p>
<p><strong>方式1：</strong></p>
<p>   <code>Program(&#39;程序名&#39;, Split(&#39;程序文件序列* *空格分隔&#39;))</code></p>
<p><strong>方式2：</strong></p>
<p>  <code>Program(&#39;程序名&#39;, Glob(&#39;*.cpp&#39;))</code></p>
<h2 id="4-编译的时候想添加上-g-O2-Wall参数"><a href="#4-编译的时候想添加上-g-O2-Wall参数" class="headerlink" title="4. 编译的时候想添加上-g -O2 -Wall参数"></a>4. 编译的时候想添加上-g -O2 -Wall参数</h2><p>使用环境变量</p>
<p> <code>env=Environment(CCFLAGS = [&#39;-g&#39;,&#39;-O3&#39;,&#39;-Wall&#39;])</code> </p>
<p>生成程序的时候使用env.Program(。。。。)即可<br>例如：</p>
  <figure class="highlight dockerfile"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></div></td><td class="code"><pre><code class="hljs dockerfile"><span class="hljs-keyword">env</span>=Environment(CCFLAGS = [<span class="hljs-string">&#x27;-g&#x27;</span>,<span class="hljs-string">&#x27;-O3&#x27;</span>,<span class="hljs-string">&#x27;-Wall&#x27;</span>])<br><span class="hljs-keyword">env</span>.Program(<span class="hljs-string">&#x27;程序名&#x27;</span>, Glob(<span class="hljs-string">&#x27;*.cpp&#x27;</span>))<br></code></pre></td></tr></table></figure>

<h2 id="5-编译生成静态库"><a href="#5-编译生成静态库" class="headerlink" title="5. 编译生成静态库"></a>5. 编译生成静态库</h2><p>  <strong>方法1：使用Library关键字</strong></p>
<p>  <code>Library(&#39;库名&#39;, Split(&#39;程序文件序列,空格分隔&#39;)</code></p>
<p>  或</p>
<p>  <code>Library(&#39;库名&#39;, Glob(&#39;\*.cpp&#39;))</code></p>
<p>  <strong>方法2：使用显示关键字StaticLibrary</strong></p>
<p>  <code>StaticLibrary(&#39;库名&#39;, Split(&#39;程序文件序列,空格分隔&#39;))</code></p>
<p>  或</p>
<p>  <code>StaticLibrary(&#39;库名&#39;, Glob(&#39;\*.cpp&#39;))</code></p>
<p>  说明：库名不要前面的lib和后缀，如库libtestdota.a生成时的库名为testdota</p>
<h2 id="6-编译生成动态库"><a href="#6-编译生成动态库" class="headerlink" title="6. 编译生成动态库"></a>6. 编译生成动态库</h2><p>  使用关键字<code>SharedLibrary</code></p>
<p>  <code>SharedLibrary(&#39;库名&#39;, Split(&#39;程序文件序列,空格分隔&#39;))</code></p>
<p>  或</p>
<p>  <code>SharedLibrary(&#39;库名&#39;, Glob(&#39;\*.cpp&#39;))</code></p>
<p>  说明：库名不要前面的lib和后缀，如库libtestso.so生成时的库名为testso</p>
<h2 id="7-程序或库中引用已有的库"><a href="#7-程序或库中引用已有的库" class="headerlink" title="7. 程序或库中引用已有的库"></a>7. 程序或库中引用已有的库</h2><p>  使用关键字LIBS和LIBPATH</p>
<p>  LIBS后跟程序需要引用的库名</p>
<p>  LIBPATH后跟程序引用库的时候所需的库文件路径</p>
<p>  如：</p>
<p> <code>Program(&#39;testPro&#39;,&#39;prog.c&#39;, LIBS = [&#39;m&#39;,&#39;m2&#39;], LIBPATH = [&#39;/usr/lib&#39;, &#39;/usr/local/lib&#39;])</code></p>
<p>  生成的testPro程序要引用库libm.so和libm2.so，库文件路径在/usr/lib和/usr/local/lib下</p>
<h2 id="8-指定程序编译过程中需要查找的头文件路径"><a href="#8-指定程序编译过程中需要查找的头文件路径" class="headerlink" title="8. 指定程序编译过程中需要查找的头文件路径"></a>8. 指定程序编译过程中需要查找的头文件路径</h2><p> 使用关键字CPPPATH</p>
<p> 例如：</p>
<p> <code>Program(&#39;hello&#39;,&#39;hello.c&#39;, CPPPATH = [&#39;include&#39;, &#39;/home/project/inc&#39;])</code></p>
<p> 生成hello程序的时候hello.c可能引用了其他的头文件，在编译hello.c的时候会查找./include和/home/project/inc下是否有需要的头文件</p>
<p> 到此一些常用的makefile的功能基本都有了，有没有觉得很简单那，后面会继续写一些实用的相对复杂一些的功能，希望大家能够喜欢！</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/devops/">devops</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/scons/">scons</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2018/05/03/04DevOps/07%E6%9C%AC%E5%9C%B0%E6%96%87%E4%BB%B6%E6%98%A0%E5%B0%84%E4%B8%BA%E7%A3%81%E7%9B%98/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">本地文件映射为磁盘</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2014/08/01/00%E7%AE%97%E6%B3%95%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/01(%E8%BD%AC%E8%BD%BD)%E4%B8%80%E8%87%B4%E6%80%A7hash%E7%AE%97%E6%B3%95(consistent%20hashing)/">
                        <span class="hidden-mobile">(转载) 一致性hash算法（consistent hashing）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
